﻿using LiveShopvnext.Helper;
using LiveShopvnext.User;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.IdentityModel.Tokens;
using System;
using System.Collections.Generic;
using System.IdentityModel.Tokens.Jwt;
using System.Linq;
using System.Security.Claims;
using System.Text;
using System.Threading.Tasks;
using Volo.Abp.Application.Dtos;
using Volo.Abp.Application.Services;
using Volo.Abp.Domain.Repositories;

namespace LiveShopvnext.Users
{
    public class UsersAppService :ApplicationService , IUsersAppService
    {
        public IDistributedCache Cache { get; set; }

        private readonly IRepository<Users, int> _users;
        public UsersAppService(IRepository<Users, int> users)
        {
            _users = users;
        }

        #region 注册
        /// <summary>
        /// 注册
        /// </summary>
        /// <param name="usersList"></param>
        /// <returns></returns>
        /// 
        [HttpPost]
        public int UsersRegister(CreateUpdateUsersDto usersList)
        {
            var list = _users.FirstOrDefault(x => x.usersPhone == usersList.usersPhone);

            if (list!=null)
            {
                return 0;  //手机号已存在
            }

            if (usersList.usersName==null||usersList.usersName=="")
            {
                usersList.usersName = usersList.usersPhone;
            }

            var data = _users.FirstOrDefault(x => x.usersName == usersList.usersName);

            if (data!=null)
            {
                return 10; //用户名已存在
            }
            
            usersList.usersPassword = StringHelper.MD5Hash(usersList.usersPassword);

            var users = new Users
            {
                usersName= usersList.usersName,
                usersPassword=usersList.usersPassword,
                usersPhone=usersList.usersPhone,
                usersSex= usersList.usersSex,
                usersEmail=usersList.usersEmail

            };

            var i = _users.InsertAsync(users);

            return i!=null?20:30; //20添加成功 30添加失败

        }
        #endregion
         
        #region 账号和密码 登录
        /// <summary>
        /// 登录
        /// </summary>
        /// <param name="usersList"></param>
        /// <returns></returns>
        [HttpGet]
        public ResultT<int> UsersRegistration(UsersLoginDto usersLogin)
        {
            //取值和转化 redis中的value是byte[]类型 我们要使用的是 string 就需要转化
            string code = Encoding.Default.GetString(Cache.Get("code" + usersLogin.snowflakeId));

            if (code==null||code=="")
            {
                return new ResultT<int> {
                        State= (int)CommunalEnum.loggingstatus.codeexpired, 
                        Message="验证码过期" 
                    };//验证码过期
            }

            if (!string.IsNullOrEmpty(usersLogin.ValidateCode)&&code.ToLower() != usersLogin.ValidateCode.ToLower())
            {
                return new ResultT<int> {   
                        State= (int)CommunalEnum.loggingstatus.codeerror, 
                        Message="验证码错误" 
                    };//验证码错误
            }

            var data = _users.FirstOrDefault(x => x.usersName == usersLogin.usersName);

            if (data == null)
            {
                return new ResultT<int> {
                        State= (int)CommunalEnum.loggingstatus.usersnotexist, 
                        Message="用户名不存在" 
                    }; //用户名不存在
            }

            if (data.usersPassword == StringHelper.MD5Hash(usersLogin.usersPassword))
            {
                return new ResultT<int> {
                        State= (int)CommunalEnum.loggingstatus.successfully, 
                        Message="登录成功",
                        Data=data.Id 
                    }; //登录成功
            }

            else
            {
                return new ResultT<int> {
                        State= (int)CommunalEnum.loggingstatus.wrongpassword, 
                        Message="密码错误" 
                    }; //密码错误
            }
        }
        #endregion

        #region 显示
        /// <summary>
        /// 显示
        /// </summary>
        /// <param name="usersList"></param>
        /// <returns></returns>
        [HttpGet]
        public PagedResultDto<UsersDto> GetUsersDto(UsersListDto usersList)
        {
            var data = from a in _users
                       select new UsersDto { 
                        usersName=a.usersName,
                        usersPhone=a.usersPhone,
                        usersEmail=a.usersEmail,
                        usersSex=a.usersSex
                       };

            var list = data.WhereIf(!string.IsNullOrWhiteSpace(usersList.usersName),x => x.usersName.Contains(usersList.usersName)).ToList() ;

            int totalCount = list.Count();

            return new PagedResultDto<UsersDto>(totalCount, list);
        }
        #endregion

        #region 手机号登录
        public ResultT<int> phonelogin(UsersLoginDto usersLogin)
        {
            //取值
            string code = Encoding.Default.GetString(Cache.Get("PhoneCode" + usersLogin.usersPhone));

            if (code==""||code==null)
            {
                return new ResultT<int> { 
                        State= (int)CommunalEnum.loggingstatus.codeexpired, 
                        Message= "验证码过期" 
                    }; //验证码过期
            }
            if (code!=usersLogin.ValidateCode)
            {
                return new ResultT<int> {
                        State= (int)CommunalEnum.loggingstatus.codeerror, 
                        Message= "验证码错误" 
                    }; //验证码错误
            }

            return new ResultT<int> { 
                    State= (int)CommunalEnum.loggingstatus.successfully, 
                    Message= "登录成功" 
                }; //登录成功


        }
        #endregion
    }
}
